package com.rui.study.algorithm.L_深入理解JVM_方法调用;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;

import static java.lang.invoke.MethodHandles.lookup;

public class MethodHandleTest {

    @Override
    public String toString() {
        return "永久String";
    }

    public static void main(String[] args) throws Throwable {
        String re = (String) getMethodHandle("777").invokeExact();
        System.out.println(re);
    }

    public static MethodHandle getMethodHandle(Object receiver) {
        MethodType mt = MethodType.methodType(String.class);
        try {
            return lookup().findVirtual(receiver.getClass(), "toString", mt).bindTo(receiver);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }
}
